﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using GCC.APIContainer.Utilities;
using GCC.Container.Abstracts;

using Microsoft.AspNetCore.Mvc;

using tcm.entity;
using tcm.request;
using tcm.response;
using tcm.utils;

namespace tcm.Controller
{
    [ApiController]
    [Route("tcm/user/phm")]
    public class UserPhmController : GCCAPIControllerBase
    {
        public UserPhmController(AbsGCCContext context) : base(context)
        {
        }

        [HttpPost("add")]
        public Result addUser(UserPhmReq req)
        {
            Result result = new Result();
            result.code = GlobalConstant.Success;
            result.msg = "新增发药成功！";

            List<User> userList = GCCEntrance.DBManager.GlobalDB.Select<User>().Where(a => a.id == req.userId).ToList();
            if (userList == null || userList.Count == 0)
            {
                result.code = GlobalConstant.Failure;
                result.msg = "新增发药用户信息为空！";
                return result;
            }

            List<PhmListRelation> reList = GCCEntrance.DBManager.GlobalDB.Select<PhmListRelation>()
                .Where(a => a.phm_list_id == req.phmListId)
                .Where(a => a.status == 1).ToList();
            if (reList == null || reList.Count == 0)
            {
                result.code = GlobalConstant.Failure;
                result.msg = "处方没有关联的药品！";
                return result;
            }
            List<UesrPhmList> list = new List<UesrPhmList>();
            for (int i = 0; i < reList.Count; i++)
            {
                UesrPhmList userPhm = new UesrPhmList();
                userPhm.id = IdUtils.getId(i);
                userPhm.user_id = req.userId;
                userPhm.phm_list_id = req.phmListId;
                userPhm.phm_id = reList[i].phm_id;
                userPhm.order_id = req.orderId;
                userPhm.create_date = DateTime.Now.ToLocalTime();
                userPhm.update_date = DateTime.Now.ToLocalTime();

                list.Add(userPhm);
            }

            int m = GCCEntrance.DBManager.GlobalDB.Insert(list).ExecuteAffrows();
            if (m == 0)
            {
                result.code = GlobalConstant.Failure;
                result.msg = "入库失败！";
            }
            else
            {
                result.code = GlobalConstant.Success;
                result.msg = "更新成功！";
            }
            return result;
        }

        [HttpDelete("cancel")]
        public Result cancel(UserPhmReq req)
        {
            Result result = new Result();
            result.code = GlobalConstant.Success;
            result.msg = "取消发药成功！";

            List<User> userList = GCCEntrance.DBManager.GlobalDB.Select<User>().Where(a => a.id == req.userId).ToList();
            if (userList == null || userList.Count == 0)
            {
                result.code = GlobalConstant.Failure;
                result.msg = "用户信息为空！";
                return result;
            }

            List<UesrPhmList> userPhmList = GCCEntrance.DBManager.GlobalDB.Select<UesrPhmList>().Where(a => a.order_id == req.orderId).ToList();
            if (userPhmList == null || userPhmList.Count == 0)
            {
                result.code = GlobalConstant.Success;
                result.msg = "订单下面没有药品信息！";
                return result;
            }

            int m = GCCEntrance.DBManager.GlobalDB.Delete<UesrPhmList>().Where(a => a.order_id == req.orderId).ExecuteAffrows();
            if (m == 0)
            {
                result.code = GlobalConstant.Failure;
                result.msg = "入库失败！";
            }
            else
            {
                result.code = GlobalConstant.Success;
                result.msg = "取消成功！";
            }

            return result;
        }
    }
}
